﻿/**
*    @file:    progresssavedialog.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-01-03  18:35
*/

#include "progresssavedialog.h"
#include "ui_progresssavedialog.h"

#include <QFileDialog>
#include <QFileInfo>

ProgressSaveDialog::ProgressSaveDialog(Progress *progress, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ProgressSaveDialog)
{
    ui->setupUi(this);
    progressInfo = progress;
    if (progressInfo->getId() != 0)
    {
        setWindowTitle("修改进程");
        ui->path->setText(progressInfo->getPath());
        ui->name->setText(progressInfo->getName());
        ui->vanish_up->setChecked(progressInfo->getVanish_up());
        ui->cpu_use_up->setChecked(progressInfo->getCpu_use_up());
        ui->cpu_use->setValue(progressInfo->getCpu_level());
        ui->heartbeat_up->setChecked(progressInfo->getHeartbeat_up());
        ui->heartbeat_msg->setText(progressInfo->getHeartbeat_msg());
        ui->heartbeat_timeout_second->setValue(progressInfo->getHeartbeat_timeout_second());
        ui->init_start->setChecked(progressInfo->getInit_start());
        ui->start_sleep->setValue(progressInfo->getStart_sleep());
        ui->enable->setChecked(progressInfo->getDaemon_open());
    }
    else
    {
        setWindowTitle("添加进程");
    }
}

ProgressSaveDialog::~ProgressSaveDialog()
{
    delete ui;
}

void ProgressSaveDialog::on_saveBtn_clicked()
{
    progressInfo->setPath(ui->path->text());
    progressInfo->setName(ui->name->text());
    progressInfo->setVanish_up(ui->vanish_up->isChecked());
    progressInfo->setCpu_use_up(ui->cpu_use_up->isChecked());
    progressInfo->setCpu_level(ui->cpu_use->value());
    progressInfo->setHeartbeat_up(ui->heartbeat_up->isChecked());
    progressInfo->setHeartbeat_msg(ui->heartbeat_msg->text());
    progressInfo->setHeartbeat_timeout_second(ui->heartbeat_timeout_second->value());
    progressInfo->setInit_start(ui->init_start->isChecked());
    progressInfo->setStart_sleep(ui->start_sleep->value());
    progressInfo->setDaemon_open(ui->enable->isChecked());
    QDialog::accept();
}

void ProgressSaveDialog::on_cancelBtn_clicked()
{
    QDialog::reject();
}

void ProgressSaveDialog::on_selBtn_clicked()
{
    QString path = ui->path->text();
    QString openDir = "";
    if (!path.isEmpty())
    {
        openDir = QFileInfo(path).path();
    }
    path = QFileDialog::getOpenFileName(this, QString("选择文件"), openDir, QString("应用程序(*.exe);;所有文件(*.*)"));
    if (!path.isEmpty())
    {
        ui->path->setText(path);
        QFileInfo fileInfo(path);
        if (ui->name->text().isEmpty())
            ui->name->setText(fileInfo.fileName());
        ui->heartbeat_msg->setText(fileInfo.baseName() + "_heartbeat");
    }
}
